package com.wwtech.epp.ui.activity.work.presenter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.LoadFile;
import com.wwtech.epp.entity.ProjectEntity;
import com.wwtech.epp.entity.UserInfo;
import com.wwtech.epp.ui.activity.work.NewTaskFragment;
import com.wwtech.epp.ui.activity.work.contract.NewTaskContract;
import com.wwtech.epp.utils.ToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.wwtech.epp.ui.activity.work.NewProjectActivity.RESULTOK;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of NewTaskFragment
 * @date 2017/09/16 16:06:07
 */
public class NewTaskPresenter implements NewTaskContract.NewTaskContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final NewTaskContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private NewTaskFragment mFragment;

    @Inject
    public NewTaskPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull NewTaskContract.View view, NewTaskFragment fragment) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mFragment = fragment;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == NewTaskFragment.CHECKMANAGE && resultCode == RESULTOK) {
            mView.onProjectManagerBack(ContainValue.projectManager);
        }

    }

    @Override
    public void getProjectListFromServer(String projectStatus, int requestPage, int onePageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", onePageSize + "");
        map.put("pageNum", requestPage + "");
        Disposable disposable = httpAPIWrapper.getProjectOwn(ContainValue.userLogin.getData().getUserId() + "", map)
                .subscribe(new Consumer<ProjectEntity>() {
                    @Override
                    public void accept(ProjectEntity projectEntity) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.setListData(projectEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void newTask(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.jobAdd(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.jobAddSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void upLoadFile(String fileName, String filePath, int position) {
        mView.showProgressDialog();
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File((filePath)));
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("filename", fileName, fileRequestBody);
        Disposable disposable = httpAPIWrapper.uploadFile(filePart)
                .subscribe(new Consumer<LoadFile>() {
                    @Override
                    public void accept(LoadFile loadFile) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.loadFileSuccess(position, loadFile.getData().getUrl());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        mView.closeProgressDialog();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}